package com.gf.search.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gf.base.controller.BasicController;
import com.gf.common.bean.DataRequest;
import com.gf.common.bean.DataResponse;
import com.gf.common.bean.Page;
import com.gf.common.constant.ViewPaths;
import com.gf.common.staticer.MessageRemainsHelper;
import com.gf.common.util.WebHelper;
import com.gf.message.service.MessageService;
import com.gf.search.service.SearchService;
import com.gf.user.model.User;

@Controller
@RequestMapping("/search")
public class SearchController extends BasicController {

	private static final Log logger = LogFactory.getLog(SearchController.class);

	@Autowired
	private SearchService searchService;

	@Autowired
	private MessageService messageService;

	/**
	 * 搜索页面
	 * 
	 * @param keyword
	 * @param type
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public ModelAndView search() throws UnsupportedEncodingException {
		ModelAndView mvc = new ModelAndView();

		User user = WebHelper.getSessionUser();
		if (user != null) {
			int messageCount = MessageRemainsHelper.getRemains(user.getId());
			mvc.addObject("messageCount", messageCount);
		}
		mvc.addObject("user", user);
		mvc.setViewName(ViewPaths.SEARCH_INDEX);

		return mvc;
	}

	/**
	 * 搜索结果返回
	 * 
	 * @param keyword
	 * @param type
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/index", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse searchRequest(DataRequest dataRequest,
			HttpServletRequest request, String type)
			throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");

		if (request.getParameterValues("keyword") == null) {
			return new DataResponse();
		}

		String keyword = request.getParameterValues("keyword")[0];
		keyword = URLDecoder.decode(keyword, "UTF-8");

		logger.info("搜索请求:keyword=" + keyword + " type=" + type);

		DataResponse dataResponse = new DataResponse();
		Page page = new Page();

		int curpage = dataRequest.getPage();
		int pageSize = dataRequest.getRows();

		// 用户搜索
		if (type.equals("user")) {
			page = searchService.searchForUser(keyword, curpage, pageSize);
		}
		// 项目搜索
		else if (type.equals("exam")) {
			page = searchService.searchForExam(keyword, curpage, pageSize);
		}
		// 帮助
		else if (type.equals("question")) {
			page = searchService.searchForQuestion(keyword, curpage, pageSize);
		}

		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());

		return dataResponse;
	}

}
